<?php


class Order extends MY_View_Controller
{
    function __construct(){
        parent::__construct();
        $this->load->config('order');
    }
    /**
     * 获取订单列表  GET
     *
     * @return json
     */
    function gets(){
        $page = $this->input->get('page');
        $page = $page>0?$page:1;
        $this->load->model('order/Orders_model');
        $condition = array(
            'is_del'    => 0,
            'user_id'   => $this->_login_user['user_id']);
        $status = $this->input->get('status');
        if($status)
            $condition['status'] = $status;
        $base_url = url_query($condition,'/user/order/gets/');
        $order = $this->input->get('order');
        $sort = $this->input->get('sort');
        if($order and $sort)
            $order = $order.' '.$sort;
        else
            $order = 'id desc';
        $pagination = $this->Orders_model->pagination($base_url, $page, $condition, $order);
        $this->assign('orders',$pagination);
        $this->display();
    }


    /**
     * 取消订单  POST
     *
     * @return json
     */
    function cancel(){
        $order_id = $this->input->post('order_id');
        if(!$order_id)
            $this->json_response(false);
        $this->load->model('order/Orders_model');
        //只允许取消状态小于4的，管理员可以取消别的级别的
        $order = $this->Orders_model->getOne(array('order_id'=>$order_id,'status<'=>4));
        if(!$order)
            $this->json_response(false,'','不允许取消状态');
        $this->Orders_model->refund();
        $this->json_response(true);
    }

    /**
     * 提交订单  POST
     *
     * @return json
     */
    function do_place(){
        $activity_id =  intval($this->input->post('activity_id'));
        $user_id =  intval($this->_login_user['user_id']);
        $remarks =  $this->input->post('remarks');
        $alternate_person = $this->input->post('alternate_person');
        $alternate_tel    = $this->input->post('alternate_tel');
        $participation    = $this->input->post('participation');

        if(!$activity_id  or !$alternate_tel or !$alternate_person or !$participation)
            $this->json_response(false);//10800
        $this->load->model('activities/Activities_model');
        $activity = $this->Activities_model->getOne(array('id'=>$activity_id));
        //计算价格
        if(isset($activity['price'])){
            $price = $participation*$activity['price'];
        }
        else{
            if(isset($activity['sale_price'])){
                $arr = explode('/',$activity['sale_price']);
                if(!($participation==$arr[1]))
                    $this->json_response(false);//30200
                else
                     $price = $arr[0];
            }
        }
        $this->load->helper('string');
        $num = random_string('numeric',6);
        $data = array(
            'order_id'          =>  date('YmdHis',time()).$num,
            'activity_id'           =>  $activity_id,
            'user_id'           =>  $user_id,
            'remarks'            =>  $remarks,
            'alternate_person'  =>  $alternate_person,
            'alternate_tel'     =>  $alternate_tel,
            'participation'     =>  $participation,
            'price'             =>  $price,
            'created'        =>  date('Y-m-d H:i:s',time())
        );
        $this->load->model('order/Orders_model');
        $insert_id = $this->Orders_model->insert($data);
        if(!$insert_id){
            $this->json_response(false);//10500
        };
        $this->json_response(true,'',$insert_id);
    }

    /**
     * 完善出行人信息  POST
     * example:order_id:20151006125352bv
     * {trip_person:[{name:”张三”,gender,”1”,
     * identify_num:”430221198825147514”,
     * mobile:”13333333333”},trip_person:
     * [{name:”张si”,gender,”1”,identify_num:”430221198825147514”,
     * mobile:”13333333333”}......]}
     *
     * @return json
    function do_perfect(){
        $order_id = $this->input->post('order_id');
        $user_id = $this->_login_user['user_id'];
        $realname = $this->input->post('realname');
        $identify_num = $this->input->post('indentify_num');
        $gender = $this->input->post('gender');
        $mobile = $this->input->post('mobile');
        if(!$order_id or !$realname or !$identify_num or !$gender or !$mobile)
            $this->json_response(false,null,'20002');
        $data = array(
            'order_id'      => $order_id,
            'user_id'       =>  $user_id,
            'realname'      =>  $realname,
            'identify_num'  =>  $identify_num,
            'gender'        =>  $gender,
            'mobile'        =>  $mobile
        );
        $this->load->model('order/Order_traveller');
        if(!($insert_id = $this->order_traveller->insert($data))){
            $this->set_response(false,null,'30503');
        };
        $this->json_response(true,$insert_id);
    }
     */
}